'use strict';

import config from 'config-lite' // config-lite 是一个轻量的读取配置文件的模块
import formidable from 'formidable'
import AdminModel from '../../models/admin/admin'
import BaseComponent from '../../prototype/baseComponent'
import crypto from 'crypto'

class Admin extends BaseComponent {
    constructor() {
        super()
        this.login = this.login.bind(this)
        this.register = this.register.bind(this)
    }
    async login(req, res, next) {
        res.send({
            status: config.status.httpCodeSucceed,
            type: 'FORM_DATA_ERROR',
            message: '表单信息错误'
        })
    }
    async register(req, res, next) {
        const form = new formidable.IncomingForm()
        form.parse(req, async (err, fields, files) => {
            if (err) {
                res.send({
                    status: config.status.httpCodeServerError,
					type: 'FORM_DATA_ERROR',
					message: '表单信息错误'
                })
                return
            }
            const { user_name, password, status = 1 } = fields;
            try {
                if (!user_name) {
					throw new Error('用户名错误')
				}else if(!password){
					throw new Error('密码错误')
				} 
            } catch(error) {
                console.log(error.message, error);
				res.send({
					status: 0,
					type: 'GET_ERROR_PARAM',
					message: error.message
				})
				return
            }
            try {
                const admin = await AdminModel.findOne({ user_name })
                if(admin) {
                    console.log('该用户已经存在')
					res.send({
						status: config.status.httpCodeServerError,
						type: 'USER_HAS_EXIST',
						message: '该用户已经存在'
					})
                } else {
                    const newpassword = this.encryption(password)
                    const admin_id = await this.getId('admin_id')
                    const times = new Date().getTime()
                    const newAdmin = {
                        user_name,
                        password: newpassword,
                        id: admin_id,
                        create_time: times,
                        admin: '用户',
                        status,
                        city: '上海'
                    }
                    await AdminModel.create(newAdmin)
                    req.session.admin_id = admin_id
                    res.send({
                        status: config.status.httpCodeSucceed,
                        success: '注册成功'
                    })
                }
            } catch(error) {
                console.log('注册管理员失败', error)
                res.send({
                    status: config.status.httpCodeServerError,
					type: 'FORM_DATA_ERROR',
					message: '注册失败'
                })
            }
        })
    }
    encryption(password){
		const newpassword = this.Md5(this.Md5(password).substr(2, 7) + this.Md5(password));
		return newpassword
	}
	Md5(password){
		const md5 = crypto.createHash('md5');
		return md5.update(password).digest('base64');
	}
}

export default new Admin()